#include <QTreeWidget>
#include <QSplitter>
#include <QListWidget>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QFileInfo>
#include <QDir>
#include <QDebug>

#include "imageview.h"
#include "../constants.h"

namespace Jcr
{
class ImageViewPrivate
{
public:
    QTreeWidget* tree = nullptr;
    QListWidget* list = nullptr;
    QMenu* contextMenu = nullptr;
    QString rootPath;
};

static const int ColumnIndex=  0;
ImageView::ImageView(QWidget* parent) : AbstractView(parent)
{
    d_ptr = new ImageViewPrivate;
    init();
}

ImageView::~ImageView()
{
    delete d_ptr;
}

void ImageView::setRootPath(const QString& root)
{
    if (d_ptr->rootPath != root) {
        d_ptr->rootPath = root;

    }
}

QString ImageView::rootPath() const
{
    return d_ptr->rootPath;
}

void ImageView::reload()
{
    if (d_ptr->rootPath.isEmpty())
        return;

    d_ptr->tree->clear();

    QFileInfo fi(d_ptr->rootPath);
    QTreeWidgetItem* rootItem = new QTreeWidgetItem();
    rootItem->setText(ColumnIndex, fi.baseName());
    rootItem->setToolTip(ColumnIndex, QDir::toNativeSeparators(d_ptr->rootPath));
    d_ptr->tree->addTopLevelItem(rootItem);

    readFolder(rootItem, d_ptr->rootPath);
    d_ptr->tree->expandAll();
}

void ImageView::init()
{
    setWindowTitle(Jcr::Title::IMAGEVIEW);

    d_ptr->tree = new QTreeWidget(this);
    d_ptr->tree->header()->hide();
    d_ptr->tree->setAlternatingRowColors(true);

    d_ptr->list = new QListWidget(this);
    d_ptr->list->setViewMode(QListWidget::IconMode);
    d_ptr->list->setIconSize(QSize(300, 200));

    QVBoxLayout* layout = new QVBoxLayout;
    QSplitter* splitter = new QSplitter(Qt::Horizontal, this);
    splitter->addWidget(d_ptr->tree);
    splitter->addWidget(d_ptr->list);

    QList<int> sizes;
    sizes.append(100);
    sizes.append(400);
    splitter->setSizes(sizes);

    layout->addWidget(splitter);
    setLayout(layout);

    createContextMenu();

    connect(d_ptr->tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(onItemDoubleClicked(QTreeWidgetItem*,int)));
}

void ImageView::contextMenuEvent(QContextMenuEvent *e)
{

}

void ImageView::onItemDoubleClicked(QTreeWidgetItem* item, int column)
{
    Q_UNUSED(column)
    d_ptr->list->clear();
    const QString folder = item->toolTip(ColumnIndex);
    QDir imageFolder(folder);
    QFileInfoList images = imageFolder.entryInfoList(QStringList() << "*.png" << "*.jpg", QDir::Files);
    foreach (auto image, images) {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(image.filePath()));
        d_ptr->list->addItem(item);
    }
}

void ImageView::readFolder(QTreeWidgetItem* parentItem, const QString &folder)
{
    QDir topDir(folder);
    QStringList subFolders = topDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    if (subFolders.isEmpty())
        return;

    foreach (auto f, subFolders) {
        QFileInfo fi(f);
        QTreeWidgetItem* folderItem = new QTreeWidgetItem;
        folderItem->setToolTip(ColumnIndex, QDir::toNativeSeparators(folder + "/" + fi.baseName()));
        folderItem->setText(ColumnIndex, fi.baseName());
        parentItem->addChild(folderItem);
        readFolder(folderItem, QDir::toNativeSeparators(folder + "/" + f));
    }
}

void ImageView::createContextMenu()
{

}

} // namespace Jcr
